package com.pingan.pastry.service.impl;

import com.pingan.pastry.dao.UserDAO;
import com.pingan.pastry.entity.Scene;
import com.pingan.pastry.entity.User;
import com.pingan.pastry.factory.HttpInvokerFactory;
import com.pingan.pastry.factory.InvokerFactory;
import com.pingan.pastry.service.UserService;
import com.pingan.pastry.spi.UserSPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;


    @Override
    public User findUser(String email, String passWord) {
        User user = userDAO.selectUser(email, passWord);
        return user;
    }

    @Override
    public List<String> findPermission(Integer userId) {
        List<Scene> scenes = userDAO.selectScene(userId);
        List<String> permissions = new ArrayList<>();
        if (scenes != null && scenes.size() > 0) {
            for (Scene scene : scenes) {
                String resourcesStr = scene.getResources();
                String[] sceneResource = resourcesStr.split(",");
                permissions.addAll(Arrays.asList(sceneResource));
            }
        }
        return permissions;
    }

    public static void main(String[] args) {
        InvokerFactory invokerFactory = new HttpInvokerFactory();
        UserSPI userSPI = invokerFactory.build(UserSPI.class);
        List<User> userList = userSPI.listUser("001", 15);
        System.out.println(userList);
    }

}
